package com.jrx.anytxn.param.service.system.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.system.TransactionCodeTableReq;
import com.jrx.anytxn.param.dto.system.TransactionCodeTableRes;
import com.jrx.anytxn.param.entity.PrTransactionCodeTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrTransactionCodeTableMapper;
import com.jrx.anytxn.param.service.system.ITransactionCodeTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 交易码表 业务接口实现
 *
 * @author yx
 * @date 2019/5/28
 */
@Service
@CacheConfig(cacheNames = "TRANS_CODE")
public class TransactionCodeTableServiceImpl implements ITransactionCodeTableService {
    private static final Logger logger = LoggerFactory.getLogger(TransactionCodeTableServiceImpl.class);
    @Resource
    private ExtPrTransactionCodeTableMapper extPrTransactionCodeTableMapper;

    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<TransactionCodeTableRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId,String transactionCode) throws TxnException {
        Page<PrTransactionCodeTable> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrTransactionCodeTable> txnTableList = extPrTransactionCodeTableMapper.selectByTenId(tenantId,transactionCode);
        if (txnTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<TransactionCodeTableRes> res = BeanMapping.copyList(txnTableList, TransactionCodeTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public TransactionCodeTableRes addTransactionCodeTable(TransactionCodeTableReq transactionCodeTableReq) throws TxnException {
        PrTransactionCodeTable prTransactionCodeTable = extPrTransactionCodeTableMapper.selectByCodeAndTenId(transactionCodeTableReq.getTransactionCode(), transactionCodeTableReq.getTenantId());
        if (prTransactionCodeTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrTransactionCodeTable transactionCodeTable = BeanMapping.copy(transactionCodeTableReq, PrTransactionCodeTable.class);
        try {
            transactionCodeTable.setId(segmentService.getId(Constant.PR_TRANSACTION_CODE_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }

        transactionCodeTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrTransactionCodeTableMapper.insertSelective(transactionCodeTable);
        return BeanMapping.copy(transactionCodeTable, TransactionCodeTableRes.class);
    }

    @CacheEvict(key="'transCode_'+#transactionCodeTableReq.tenantId+'_'+#transactionCodeTableReq.transactionCode")
    @Override
    public TransactionCodeTableRes modifyTransactionCodeTable(TransactionCodeTableReq transactionCodeTableReq) throws TxnException {
        PrTransactionCodeTable prTransactionCodeTable = extPrTransactionCodeTableMapper.selectByCodeAndTenId(transactionCodeTableReq.getTransactionCode(), transactionCodeTableReq.getTenantId());
        if (prTransactionCodeTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrTransactionCodeTable transactionCodeTable = BeanMapping.copy(transactionCodeTableReq, PrTransactionCodeTable.class);
        // 更新
        extPrTransactionCodeTableMapper.updateByPrimaryKeySelectiveAndNotNull(transactionCodeTable);
        return BeanMapping.copy(transactionCodeTable, TransactionCodeTableRes.class);

    }

    @CacheEvict(key="'transCode_'+#tenantId+'_'+#transactionCode")
    @Override
    public Boolean removeByCodeAndTenId(String transactionCode, String tenantId) throws TxnException {
        if (StringUtils.isBlank(transactionCode) || StringUtils.isBlank(tenantId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrTransactionCodeTableMapper.deleteByCodeAndTenId(transactionCode, tenantId);
        return i > 0;
    }

    @Override
    public TransactionCodeTableRes findByCodeAndTenId(String transactionCode, String tenantId) throws TxnException {
        if (StringUtils.isBlank(transactionCode) || StringUtils.isBlank(tenantId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrTransactionCodeTable txnCodeTable = extPrTransactionCodeTableMapper.selectByCodeAndTenId(transactionCode, tenantId);
        return BeanMapping.copy(txnCodeTable, TransactionCodeTableRes.class);
    }

    @Cacheable(key="'transCode_'+#tenantId+'_'+#transactionCode")
    @Override
    public PrTransactionCodeTable findByCodeAndTenantId(String transactionCode, String tenantId) throws TxnBizException {
        PrTransactionCodeTable txnCodeTable = extPrTransactionCodeTableMapper.selectByCodeAndTenId(transactionCode, tenantId);
        if(txnCodeTable == null){
            String msg = String.format(ErrorMsgConstant.NO_TRANSCATION_CODE,transactionCode);
            logger.error(msg);
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), msg);
        }
        return txnCodeTable;
    }

    @Override
    public List<TransactionCodeTableRes> findByTenId(String tenantId) {
        List<PrTransactionCodeTable> prOrganizationTables = extPrTransactionCodeTableMapper.selectByTenId(tenantId,null);
        return BeanMapping.copyList(prOrganizationTables, TransactionCodeTableRes.class);
    }
}
